<?php

namespace Product\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Product\Form\CategoryDetailForm;
use Product\Model\Category;
use Product\Form\CategoryForm;

class CategoryController extends AbstractActionController
{
    protected $categoryTable;
	
	public function indexAction()
	 {
		return new ViewModel(array(
             'categories' => $this->getCategoryTable()->fetchAll(),
        ));
	 }
	 
	 public function getCategoryTable()
     {
         if (!$this->categoryTable) {
             $sm = $this->getServiceLocator();
             $this->categoryTable = $sm->get('Product\Model\CategoryTable');
         }
         return $this->categoryTable;
     }
	

	public function addAction()
	 {
		$form = new CategoryForm(); 
        $form->get('submit')->setValue('Save');
		$request = $this->getRequest(); 

		if($request->isPost()) 
		{ 
		$product = new Category(); 
		$form->setInputFilter($product->getInputFilter()); 
		$form->setData($request->getPost()); 
						 
			if($form->isValid())
			{ 
				// echo 'valid';
				$product->exchangeArray($form->getData()); 
				$this->getCategoryTable()->saveCategory($product);
				$this->redirect()->toRoute('productcategory');
			}
			echo $product->name;
			$testing = $form->getData();
			echo $testing['name'];
				
		}return array('form' => $form);
	 }
	 
	public function detailAction()
    {
		$id = (int) $this->params()->fromRoute('id', 0);
         if (!$id) {
             return $this->redirect()->toRoute('productcategory', array(
                 'action' => 'index'
             ));
         }

         // Get the Album with the specified id.  An exception is thrown
         // if it cannot be found, in which case go to the index page.
         try {
             $product = $this->getCategoryTable()->getCategory($id);
         }
         catch (\Exception $ex) {
             return $this->redirect()->toRoute('product/category', array(
                 'action' => 'index'
             ));
         }

         $form  = new CategoryDetailForm();
         $form->bind($product);
		 
		 return array(
             'id' => $id,
             'form' => $form,
         );
	}
	
	public function deleteAction()
    {
         $id = (int) $this->params()->fromRoute('id', 0);
         if (!$id) {
             return $this->redirect()->toRoute('productcategory');
         }

         $request = $this->getRequest();
         if ($request->isPost()) {
             $del = $request->getPost('del', 'No');

             if ($del == 'Yes') {
                 $id = (int) $request->getPost('id');
                 $this->getCategoryTable()->deleteCategory($id);
             }

             return $this->redirect()->toRoute('productcategory');
         }

         return array(
             'id'    => $id,
             'category' => $this->getCategoryTable()->getCategory($id)
         );
    }
	
	public function editAction()
     {
         $id = (int) $this->params()->fromRoute('id', 0);
         if (!$id) {
             return $this->redirect()->toRoute('productcategory', array(
                 'action' => 'add'
             ));
         }

         // Get the Album with the specified id.  An exception is thrown
         // if it cannot be found, in which case go to the index page.
         try {
             $product = $this->getCategoryTable()->getCategory($id);
         }
         catch (\Exception $ex) {
             return $this->redirect()->toRoute('productcategory', array(
                 'action' => 'index'
             ));
         }

         $form  = new CategoryForm();
         $form->bind($product);
         $form->get('submit')->setAttribute('value', 'Edit');

         $request = $this->getRequest();
         if ($request->isPost()) {
             $form->setInputFilter($product->getInputFilter());
             $form->setData($request->getPost());

             if ($form->isValid()) {
                 $this->getCategoryTable()->saveCategory($product);

                 // Redirect to list of albums
                 return $this->redirect()->toRoute('productcategory');
             }
         }

         return array(
             'id' => $id,
             'form' => $form,
         );
     }
	
}